package asn.match.dao;

import java.util.List;

import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import asn.match.model.FriendRelation;

@Repository("friend_relation")
@PersistenceContext(type = PersistenceContextType.EXTENDED)
@Transactional
public class FriendRelationDaoImpl implements FriendRelationDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public void addFriendRelation(FriendRelation friendRelation) {
		this.sessionFactory.getCurrentSession().save(friendRelation);
	}

	@Override
	public void updateFriendRelation(FriendRelation friendRelation) {
		this.sessionFactory.getCurrentSession().saveOrUpdate(friendRelation);
	}

	@Override
	public void deleteFriendRelation(FriendRelation friendRelation) {
		this.sessionFactory.getCurrentSession().delete(friendRelation);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<FriendRelation> findAllFriendRelations() {
		return (List<FriendRelation>) this.sessionFactory.getCurrentSession().createQuery("from FriendRelation friend_relation").list();
	}

	@Override
	public FriendRelation findFriendRelationById(Integer friendRelationId) {
		return (FriendRelation) this.sessionFactory.getCurrentSession().get(FriendRelation.class, friendRelationId);
	}
	
}
